<?php

class Dcmp_Web_Router extends Kd_Web_Router {

	private $_domain = NULL;
    private $_component = NULL;
    private $_controller = NULL;
    private $_action = NULL;

    public function __construct() {
        parent::__construct();
    }
    
    protected function initialize(){}

    public function getComponent() {
        return $this->_component;
    }

    public function getController() {
        return $this->_controller;
    }

    public function getAction() {
        return $this->_action;
    }
    
    public function getDomain(){
    	return $this->_domain;
    }
    
    public function setDomain($value){
    	$this->_domain = $value;
    }
    
    public function getDomainUrl() {
    	if($this->getDomain() == '/'){
    		return $this->getBaseUrl();
    	}
        return $this->getBaseUrl().$this->getDomain().'/';
    }    
    
    public function route($url = NULL){
        if(is_null($url)){
    		$url = $_SERVER["REQUEST_URI"];
    	}    	
    	$info = $this->parseUrl($url);
    	$this->_component = $info['query']['component'];
    	$this->_controller = $info['query']['controller'];
    	$this->_action = $info['query']['action'];
    	unset($info['query']['component']);
    	unset($info['query']['controller']);
    	unset($info['query']['action']);
        $_GET = array_merge($info['query'], $_GET);
        $_REQUEST = array_merge($_GET, $_REQUEST);
        return $this;
    }

    public function getActionPath() {
        return $this->getComponent() . '/' . $this->getController() . '/' . $this->getAction();
    }

    public function buildActionUrl($arguments, $action = '*', $controller = '*', $component = '*'){
        return $this->buildUrl("$component/$controller/$action", $arguments);
    }

    public function buildUrl($fragment, $arguments = array()){
        $arr = explode('/', $fragment);
        $arrLen = count($arr);
        if($arrLen == 3){
	        $arr = array(
	            $arr[0] == '*' ? $this->getComponent() : $arr[0],
	            $arr[1] == '*' ? $this->getController() : $arr[1],
	            $arr[2] == '*' ? $this->getAction() : $arr[2]
	        );        	
    	}elseif($arrLen == 2){
	        $arr = array(
	            $this->getComponent(),
	            $arr[0] == '*' ? $this->getController() : $arr[0],
	            $arr[1] == '*' ? $this->getAction() : $arr[1]
	        );    		
        }elseif($arrLen == 1){
	        $arr = array(
	            $this->getComponent(),
	            $this->getController(),
	            $arr[0] == '*' ? $this->getAction() : $arr[0]
	        );
        }elseif($arrLen == 0){
 	        $arr = array(
	        	$this->getComponent(),
	        	$this->getController(),
	        	$this->getAction()
	        );
        }        
        
        $argUrl = '';
        if (!empty($arguments) && is_array($arguments)) {
            $newArgs = $this->buildUrlArguments($arguments);
            $argUrl = '/' . str_replace('=', '/', str_replace('&', '/', urldecode(http_build_query($newArgs))));
        }

        $baseUrl = $this->getBaseUrl();
        $domainUrl = $this->getDomain();
        if(substr($baseUrl, -1) != '/'){
            $baseUrl .= '/';
        }
        if($domainUrl == $baseUrl){
            $domainUrl = '';
        }

        return str_replace('//', '/', $baseUrl . $domainUrl . '/' . implode('/', $arr) . $argUrl);
    }

    public function parseUrl($url) {
        $result = parent::parseUrl($url);
        $url = $result['path'];
        if (!empty($url) && $url[0] == '/') {
			$url = substr($url, 1);
		}
                    
        if($this->getDomain() != '/'){
        	$url = substr($url, strlen($this->getDomain()));
        }

        $arguments = array();
        if (empty($url) || $url == '/') {
            $arguments['component'] = 'index';
            $arguments['controller'] = 'index';
            $arguments['action'] = 'index';
        } else {
            if (substr($url, -1) == '/') {
                $url = substr($url, 0, -1);
            }
            if ($url[0] == '/') {
				$url = substr($url, 1);
			}
            $arr = explode('/', trim($url));
            $arguments['component'] = array_shift($arr);
            $arguments['controller'] = array_shift($arr);
            $arguments['action'] = array_shift($arr);
            $argUrl = '';
            while (count($arr) > 0) {
                if (strlen($argUrl) > 0) {
                    $argUrl .= '&';
                }
                $key = array_shift($arr);
                $val = array_shift($arr);
                $argUrl .= $key . '=' . $val;
            }

			$output = NULL;
            parse_str($argUrl, $output);
            $arguments = array_merge($output, $arguments);
        }
        
        $result['query'] = array_merge($arguments, $result['query']);
        return $result;
    }
}